var express = require("express"),
	path = require("path"),
	ejs = require("ejs"),
	ejsExtend = require("./app/ejsExtend/ejsExtend"),
	app = express(),
	//cookie
	cookieParser = require('cookie-parser'),
	//session
	session = require('express-session'),
	MongoStore = require('connect-mongo')(session),
	//解析json中间件
	bodyParser = require('body-parser'),
	config = require("./config/config"),
	//编辑器
	ueditor = require("ueditor"),
	mongoose = require("mongoose"),
	db = mongoose.connection;
	//数据库连接
	mongoose.connect(config.host+"/"+config.db);
	//数据路连接出错
	db.on('error', function (err){
	    console.log("数据连接出错"+err);
	});
	app.locals.ejsExtend = ejsExtend;
	//index
var index = require("./app/controllers/webSite/index"),
	//文章列表
	article = require("./app/controllers/webSite/article"),
	//插件首页
	PlugIn = require("./app/controllers/webSite/plug-in"),
	//插件详情
	PlugInDetails = require("./app/controllers/webSite/plug-in-details"),
	//插件详情
	gossip = require("./app/controllers/webSite/gossip"),
	//留言
	message = require("./app/controllers/webSite/message"),
	//登录
	login = require("./app/controllers/webSite/login"),
	//注册
	register = require("./app/controllers/webSite/register"),
	//搜索
	search = require("./app/controllers/webSite/search"),
	//文件上传
	uploadfile = require("./app/controllers/webSite/uploadfile");
	
	//admin
var adminLogin = require("./app/controllers/adminSite/login");
	adminIndex = require("./app/controllers/adminSite/index"),
	addUser = require("./app/controllers/adminSite/addUser"),
	//文章
	adminArticle = require("./app/controllers/adminSite/article"),
	//栏目
	column = require("./app/controllers/adminSite/column"),
	//说说
	adminGossip = require("./app/controllers/adminSite/gossip"),
	//轮播图管理
	setBanner = require("./app/controllers/adminSite/setBanner");
//加载cookie中间件
app.use(cookieParser());
//session中间件
app.use(session({
  secret: config.cookieSecret,
  key: config.db,
  cookie: {maxAge: 1000 * 60 * 60 * 24 * 30},
  store: new MongoStore({
    url : "mongodb://"+config.host+"/"+config.db
  }),
  resave: false,
  saveUninitialized: true,
}));
//json中间件
app.use(bodyParser.urlencoded({extended: false}));
app.use(bodyParser.json());
//});
//视图配置
app.engine( ".html", ejs.__express );
app.set("view engine", "html");
app.set("views", path.join(__dirname, "./app/views"));
//静态资源
app.use(express.static(path.join(__dirname, "./app/public")));
//编辑器
app.use("/ueditor/ue", ueditor(path.join(__dirname, 'public'), function(req, res, next) {
    // ueditor 客户发起上传图片请求
    if (req.query.action === 'uploadimage') {
        var foo = req.ueditor;
        var date = new Date();
        var imgname = req.ueditor.filename;
        var img_url = 'upData/ueditor/';
        res.ue_up(img_url); //你只要输入要保存的地址 。保存操作交给ueditor来做
    }
    //  客户端发起图片列表请求
    else if (req.query.action === 'listimage') {
        var dir_url = 'upData/ueditor/';
        res.ue_list(dir_url); // 客户端会列出 dir_url 目录下的所有图片
    }
    // 客户端发起其它请求
    else {
        // console.log('config.json')
        res.setHeader('Content-Type', 'application/json');
        res.redirect('/ueditor/nodejs/config.json');
    }
}));

//blog 暂时不使用 
app.use("/" , index , article , PlugIn , PlugInDetails , search , gossip , message , uploadfile , login , register);
//后台管理
app.use('/admin' , adminIndex , adminLogin , addUser , adminArticle , column , setBanner , adminGossip);
app.listen(config.port , function(){
	console.log("ok");
});
